package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    public Result changeSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新setmeal 并且返回setmeal的id
        setmealMapper.update(setmeal);
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if (dishes != null && dishes.size() > 0) {
            List<Long> ids = new ArrayList<>();

            dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
                ids.add(setmealDish.getId());
            });
            setMealDishMapper.deleteByIds(ids);
            setMealDishMapper.insertBatch(dishes);

        }
        return Result.success();

    }

    @Override
    public Result<SetmealVO> getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        BeanUtils.copyProperties(setmeal, setmealVO);
        return Result.success(setmealVO);
    }

    @Override
    public Result save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.save(setmeal);
        //返回新增的id值
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //批量添加
        setMealDishMapper.save(setmealDishes);
        return Result.success();

    }

    @Override
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return Result.success(pageResult);
    }

    @Override
    public Result status(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        if (status == StatusConstant.ENABLE) {
            //获取套餐关联的菜品
            List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
            //获取关联菜品的id
            Set<Long> dishIds =new HashSet<>();
            setmealDishes.forEach(setmealDish -> {
                dishIds.add(setmealDish.getDishId());
            });
            List<Long> dishIds1=new ArrayList<>();
            dishIds.forEach(id1->{
                dishIds1.add(id1);
            });
            //根据id 查询状态
            Integer statusCount = dishMapper.getDisableStatusCountByIds(dishIds1);
            //如果有未起售的货物
            if (statusCount > 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.status(setmeal);
        return Result.success();

    }

    @Override
    public Result deleteByIds(List<Long> ids) {

        if (setmealMapper.getStatusCountsByIds(ids) > 0)
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        else {
            setmealMapper.delete(ids);
            setMealDishMapper.deleteBySetmealIds(ids);
            return Result.success();
        }



    }
}
